<template>
  <div style="position: relative">
    <el-form
      :inline="true"
      size="mini"
      :model="formInline"
      label-position="right"
    >
      <el-form-item label="项目名称">
        <el-input
          class="filter-item"
          v-model="formInline.projectName"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="项目状态">
        <el-select
          v-model="formInline.verifyStatus"
          clearable
          placeholder="请选择"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="待立项" value="1"></el-option>
          <el-option label="立项中" value="2"></el-option>
          <el-option label="立项通过/执行中" value="3"></el-option>
          <el-option label="立项驳回" value="4"></el-option>
          <el-option label="任务完成/待归档" value="5"></el-option>
          <el-option label="归档中" value="6"></el-option>
          <el-option label="归档成功/待复盘" value="7"></el-option>
          <el-option label="归档驳回" value="8"></el-option>
          <el-option label="复盘中" value="9"></el-option>
          <el-option label="已完成" value="10"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="times"
          type="daterange"
          align="right"
          unlink-panels
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
          @change="selecttime"
        >
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="企业电话">
        <el-input
          class="filter-item"
          v-model="formInline.telephone"
          maxlength="11"
          placeholder="请输入"
        ></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">搜索</el-button>
        <el-button
          type="warning"
          class="el-button--reset"
          icon="el-icon-refresh-right"
          @click="resetFromdata"
          size="mini"
        ></el-button>
        <!-- <el-button type="primary" size="mini" @click="addFromdata"
          >项目立项</el-button
        >
        <el-button type="primary" size="mini" @click="exportForm"
          >导出</el-button
        > -->
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <div class="total" v-if="countInfo">
          <div
            class="total-item"
            :class="{ active: !formInline.verifyStatus }"
            @click="handleClickTotal('')"
          >
            全部（{{ total }}）
          </div>
          <!-- <div
            class="total-item"
            :class="{ active: formInline.verifyStatus === '1' }"
            @click="handleClickTotal('1')"
          >
            待立项（{{ countInfo.dlx || 0 }}）
          </div>
          <div
            class="total-item"
            :class="{ active: formInline.verifyStatus === '5' }"
            @click="handleClickTotal('5')"
          >
            待归档（{{ countInfo.dgd || 0 }}）
          </div>
          <div
            class="total-item"
            :class="{ active: formInline.verifyStatus === '7' }"
            @click="handleClickTotal('7')"
          >
            待复盘（{{ countInfo.dfp || 0 }}）
          </div>
          <div
            class="total-item"
            :class="{ active: formInline.verifyStatus === '9' }"
            @click="handleClickTotal('9')"
          >
            已完成（{{ countInfo.ylx || 0 }}）
          </div> -->
        </div>
        <el-table
          :data="tableData"
          size="mini"
          style="width: 100%"
          header-cell-class-name="table-header-gray"
          v-loading="isShowloading"
        >
          <el-table-column
            type="index"
            align="center"
            width="55"
            label="序号"
          ></el-table-column>
          <el-table-column
            prop="projectName"
            align="center"
            label="项目名称"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="projectCode"
            align="center"
            label="项目编码"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="customerName"
            align="center"
            label="企业名称"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="projectStatusName"
            align="center"
            label="项目状态"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{scope.row.verifyStatus | verifyStatusFilter}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="parentName"
            align="center"
            label="所在地区"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{scope.row.parentName.join('')}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="businessTypeName"
            align="center"
            label="业务类型"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="createTime"
            align="center"
            label="创建时间"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="createUserName"
            align="center"
            label="创建人"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div>{{ scope.row.createUserName }},{{ scope.row.deptName }}</div>
            </template>
          </el-table-column>

          <!-- <el-table-column prop="endDate" align="center" label="截至日期" show-overflow-tooltip></el-table-column>
          <el-table-column prop="verifyStatusName" align="center" label="审核状态" show-overflow-tooltip></el-table-column>
          <el-table-column prop="beginDate" align="center" label="签约时间" show-overflow-tooltip></el-table-column>
          <el-table-column prop="industry" align="center" label="行业" show-overflow-tooltip></el-table-column>
          <el-table-column prop="projectTotalAmount" align="center" label="项目总投资(万元)" width="120" show-overflow-tooltip></el-table-column>
          <el-table-column prop="contractAmount" align="center" label="合同额(万元)" show-overflow-tooltip></el-table-column>
          <el-table-column prop="receivedAmount" align="center" label="回款(万元)" show-overflow-tooltip></el-table-column> -->
          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="scope">
              <!-- <el-button type="text" v-if="scope.row.isProjectTaskState == 2" @click="handleApprover(scope.row)"
                >审批</el-button
              > -->
              <el-button type="text" @click="handleInfo(scope.row)"
                >查看详情</el-button
              >
              <!-- <el-button
                type="text"
                @click="handleEdit(scope.row)"
                v-if="
                  scope.row.verifyStatus != 1 && scope.row.verifyStatus !== 2
                "
                >编辑</el-button
              > -->
              <!-- v-if="getUserInfo.userId === scope.row.createUser" -->
              <!-- <el-button
                type="text"
                @click="deleteByProject(scope.row.id)"
                :disabled="getUserInfo.userId !== scope.row.createUser"
                >删除</el-button
              > -->
              <!-- <el-button
                type="text"
                @click="handleOver(scope.row)"
                v-if="
                  scope.row.verifyStatus != 1 && scope.row.verifyStatus !== 2
                "
                >完成</el-button
              > -->
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"
          ></el-pagination>
        </el-row>
      </el-col>
    </el-row>
    <!-- <el-dialog
      title="审批"
      :visible.sync="approverDialog"
      append-to-body
      width="400px"
      class="group-dialog"
    >
      <div class="approver-inquiry">
        <el-form
          :model="ruleForm"
          ref="ruleForm"
          label-width="80px"
          size="mini"
          label-position="right"
        >
          <el-form-item label="是否通过" prop="state">
            <el-radio-group v-model="ruleForm.state">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="原因" prop="approvalOpinion">
            <el-input
              class="input-item"
              v-model="ruleForm.approvalOpinion"
              placeholder="请输入"
              maxlength="20"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="approverDialog = false;resetForm()" size="small">取 消</el-button>
        <el-button type="primary" @click="approverDialog = false;submit()" size="small">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { Administration } from '@/api/projectAdministration.js';
export default {
  name: 'Administration',
  filters: {
    verifyStatusFilter (val) {
      const data = {
        1: '待立项',
        2: '立项中',
        3: '立项通过/执行中',
        4: '立项驳回',
        5: '任务完成/待归档',
        6: '归档中',
        7: '归档成功/待复盘',
        8: '归档驳回',
        9: '复盘中',
        10: '已完成',
        11: '审批完成',
        12: '复盘驳回'
      };
      return data[val];
    }
  },
  data () {
    return {
      approverDialog: false,
      formInline: {
        projectName: null,
        customerName: null,
        leaderName: null,
        telephone: null,
        businessType: null,
        groupName: null,
        areaId: null,
        startCreateTime: null,
        endCreateTime: null,
        industry: null
      },
      tableData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false,
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }
        ]
      },
      ruleForm: {
        state: 1, // 审批是否通过
        approvalOpinion: ''// 审批意见/原因
      },
      times: '',
      countInfo: {},
      approverProject: {} // 待审批项目
    };
  },
  computed: {
    ...mapGetters([
      'getUserInfo'
    ])
  },
  created () {
  },
  activated () {
    this.loadAttendanceWidgetData(); // 数据加载的方法
  },
  mounted () {
    this.loadAttendanceWidgetData();
  },
  methods: {
    // 处理审批
    handleApprover (row) {
      this.approverDialog = true;
      this.approverProject = row
    },
    resetForm () {
      this.ruleForm = {}
      this.approverProject = {}
    },
    submit () {
      let params = {
        ...this.ruleForm,
        projectId: this.approverProject.id,
        id: this.approverProject.processTaskId
      }
      Administration.projectApproval(this, params)
        .then((res) => {
          console.log(res)
          this.$message({
            message: '审批成功',
            type: 'success'
          });
          this.loadAttendanceWidgetData();
        })
    },
    // 删除项目
    // deleteByProject (id) {
    //   this.$confirm('是否删除此项目', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     let params = {
    //       id: id
    //     };
    //     return new Promise((resolve, reject) => {
    //       Administration.deleteByProject(this, params)
    //         .then((res) => {
    //           this.$message({
    //             message: '删除成功',
    //             type: 'success'
    //           });
    //           this.loadAttendanceWidgetData();
    //         })
    //         .catch((e) => {
    //           reject(e);
    //         });
    //     });
    //   }).catch(() => {

    //   })
    // },
    // 状态筛选
    handleClickTotal (val) {
      this.formInline.verifyStatus = val
      // this.verifyStatus = val
      this.pageNo = 1;
      this.loadAttendanceWidgetData()
    },
    // 选择创建时间
    selecttime (times) {
      if (times) {
        this.formInline.startCreateTime = times[0];
        this.formInline.endCreateTime = times[1];
      } else {
        this.formInline.startCreateTime = '';
        this.formInline.endCreateTime = '';
      }
    },
    /**
     * 项目管理列表
     */
    loadAttendanceWidgetData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo,
        isState: '3'
      };
      return new Promise((resolve, reject) => {
        Administration.list(this, params)
          .then((res) => {
            this.countInfo = res?.data?.jsonObject;
            this.tableData = res?.data?.data?.records;
            this.total = res?.data?.data?.total;
          })
          .catch((e) => {
            reject(e);
          });
      });
    },
    // 项目立项
    // addFromdata () {
    //   return new Promise((resolve, reject) => {
    //     Administration.checkCanAddProject(this)
    //       .then((res) => {
    //         if (res.data) {
    //           this.$router.push('/Projectsadd');
    //         } else {
    //           this.$message({
    //             showClose: true,
    //             message: '没有操作权限！',
    //             type: 'warning'
    //           });
    //         }
    //       })
    //       .catch((e) => {
    //         reject(e);
    //       });
    //   });
    // },
    // 搜索
    onSubmit () {
      this.pageNo = 1;
      this.loadAttendanceWidgetData();
    },
    // 重置
    resetFromdata () {
      this.formInline = {
        clockInTime: null,
        clockOutTime: null,
        userName: null,
        deptName: null,
        groupName: null,
        startCreateTime: null,
        endCreateTime: null,
        industry: null,
        projectName: null,
        customerName: null,
        leaderName: null,
        businessType: null,
        areaId: null
      };
      this.times = '';
      this.pageNo = 1;
      this.page_size = 10;
      this.loadAttendanceWidgetData();
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val;
      this.loadAttendanceWidgetData();
    },
    handleSizeChange (val) {
      this.page_size = val;
      this.loadAttendanceWidgetData();
    },
    currentChange (val) {
      this.pageNo = val;
      this.loadAttendanceWidgetData();
    },
    // 导出
    // exportForm () {
    //   let params = {
    //     ...this.formInline,
    //     size: this.page_size,
    //     current: this.pageNo
    //   };
    //   return new Promise((resolve, reject) => {
    //     Administration.ExportFile(this, params, '项目管理明细表.xlsx');
    //   });
    // },
    // 查看
    handleInfo (row) {
      this.$router.push({
        path: '/ProjectCcDetail',
        query: {
          projectId: row.id,
          verifyStatus: row.verifyStatus
        }
      });
    },
    // 修改
    handleEdit (row) {
      this.$router.push({
        path: '/Projectsadd',
        query: { projectId: row.id, verifyStatus: row.verifyStatus }
      });
    },
    handleOver (row) {
      // if (row.projectStatusName === '已完成') {
      let params = {
        id: row.id
      };
      return new Promise((resolve, reject) => {
        Administration.submitComplete(this, params)
          .then((res) => {
            this.$message({
              showClose: true,
              message: '操作成功！',
              type: 'success'
            });
            this.loadAttendanceWidgetData();
          })
          .catch((e) => {
            reject(e);
          });
      });
      // } else {
      //   this.$message({
      //     showClose: true,
      //     message: '所有任务完成后，才可进行完成操作！',
      //     type: 'warning'
      //   });
      // }
    }
  }
};
</script>
<style scoped lang="scss">
.total {
  display: flex;
  align-items: center;
  .total-item {
    padding: 10px;
    cursor: pointer;
    transition: color 0.3s;
    &:hover {
      color: #409eff;
    }
  }
  .active {
    color: #409eff;
  }
}
</style>
